package Main.tcp;

import Main.pojo.TcpMessage;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * PACKAGE_NAME: Main.tcp
 * Description:
 * 测试一个TCP Socket Service是否可以同时连接多个CLient
 * 功能设计：TCP 聊天室
 * 答案：可以噢
 */
public class TCPTest {
    public static void main(String[] args) throws IOException {
        TCPService tcpService = new TCPService();
    }

}


class TCPClient {
    private Socket socket;
    private String name;

    public TCPClient(String name) throws IOException {
        this.name = name;
        socket = new Socket("localhost", 8888);
    }
}

class TCPService {
    private ServerSocket serverSocket = new ServerSocket(8888);
    private List<Socket> socketList = new ArrayList<>();

    TCPService() throws IOException {
    }

    public static void main(String[] args) throws IOException {
        System.out.println("开始启动服务。");
        TCPService tcpService = new TCPService();
        tcpService.startServer();

    }

    public void pushMsg(String msg) throws IOException {
        for (Socket socket : socketList) {
            OutputStream out = socket.getOutputStream();
            out.write(msg.getBytes());
        }
    }

    public void startServer() throws IOException {
        // 键服务的监控放到线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Socket socket = serverSocket.accept();
                        // 收到信息后，再开一个线程处理
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    // 获取信息
                                    ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                                    TcpMessage tcpMessage = (TcpMessage) objectInputStream.readObject();
                                    String name = (String) tcpMessage.data;
                                    // 判断类别并处理
                                    switch (tcpMessage.code) {
                                        case 1: // 加入房间
                                            socketList.add(socket);
                                            pushMsg("系统消息：" + name + " 加入聊天室。");
                                            break;
                                        case 2: // 发送消息
                                            pushMsg(name + "：" + tcpMessage.msg);
                                            break;
                                    }
                                } catch (IOException | ClassNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

}